var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    work_time_start: '00:00',
    work_time_end: '00:00',
    dataList:[],
    autoQueue: false,
    autoNotice: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    this.loadList();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  loadList: function () {
    var that = this;
    util.getData("/atapi/loadingplatform/loadingplatform/list", {
      'CO_ID': app.globalData.CO_ID
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        that.setData({
          dataList: res.data.data
        });
      }
    });
  },
  backToList: function () {
    wx.redirectTo({
      url: '/pages/enterprise/shop'
    })
  },
  showModal(e) {
    var that = this;
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
    if (e.currentTarget.dataset.target == 'editLoadingplatform'){
      var id = e.currentTarget.dataset.id;
      var dataList = that.data.dataList;
      for (var i = 0; i < dataList.length;i++){
        if (dataList[i].ID == id){
          that.setData(dataList[i]);
          that.setData({
            autoQueue: that.data.autoQueue == 1?true:false
          });
          that.setData({
            autoNotice: that.data.autoNotice == 1 ? true : false
          });
        }
      }
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  changeManage: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/users_selected?users=' + e.currentTarget.dataset.manager + '&usersName=' + e.currentTarget.dataset.manager_name
    })
  },
  timeChange(e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  submitLoadingplatform: function () {
    var that = this;
    if (!that.data.warehouseId) {
      wx.showToast({
        title: "请选择仓库",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    if (!that.data.manager) {
      wx.showToast({
        title: "请选择管理员",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    if (isNaN(that.data.lp_number) == true || that.data.lp_number <= 0) {
      wx.showToast({
        title: "装卸口数量有误",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("/atapi/loadingplatform/loadingplatform/new", {
      'CO_ID': app.globalData.CO_ID,
      'warehouseId': that.data.warehouseId,
      'warehouse': that.data.warehouse,
      'manager': that.data.manager,
      'manager_name': that.data.manager_name,
      'lp_number': that.data.lp_number,
      'autoQueue': that.data.autoQueue?1:0,
      'autoNotice': that.data.autoNotice?1:0
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        that.setData({
          modalName: null
        })
        wx.showToast({
          title: "提交成功",
          icon: "success",
          duration: 3000,
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/loadingplatform/loading_platform'
              })
            }, 3000);
          }
        });

      } else {
        wx.showToast({
          title: "提交失败",
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  editLoadingplatform: function () {
    var that = this;
    if (!that.data.manager) {
      wx.showToast({
        title: "请选择管理员",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("/atapi/loadingplatform/loadingplatform/edit", {
      'id': that.data.ID,
      'manager': that.data.manager,
      'manager_name': that.data.manager_name,
      'autoQueue': that.data.autoQueue ? 1 : 0,
      'autoNotice': that.data.autoNotice ? 1 : 0
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        that.setData({
          modalName: null
        })
        wx.showToast({
          title: "提交成功",
          icon: "success",
          duration: 3000,
          success: function () {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/loadingplatform/loading_platform'
              })
            }, 3000);
          }
        });

      } else {
        wx.showToast({
          title: "提交失败",
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  showDetail:function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/loadingplatform/loading_platform_detail?id=' + e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
    wx.getStorage({
      key: 'manager',
      success: function (res) {
        if (res.data) {
          that.setData({
            manager: res.data.manager,
            manager_name: res.data.manager_name
          });
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'manager',
          success: function (res) {
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})